package com.rabbiter.hotel.controller.admin;

import com.rabbiter.hotel.common.CommonResult;
import com.rabbiter.hotel.common.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

@RestController("AdminRunController")
@RequestMapping("/admin/run")
@Slf4j
public class RunController {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 设置维护状态
     */
    public  static final String KEY = "run_status";
    @PutMapping("/{status}")
    public CommonResult<Integer> setStatus(@PathVariable Integer status){
        CommonResult<Integer> commonResult = new CommonResult<>();
        redisTemplate.opsForValue().set(KEY,status);
        log.info("设置系统的状态为：{}",status == 0? "运行中":"维护中");
        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        commonResult.setData(status);
        return commonResult;
    }
    /**
     * 获取系统的状态
     * @return
     */
    @GetMapping("/status")
    public CommonResult<Integer> getStatus(){
        CommonResult<Integer> commonResult = new CommonResult<>();
        Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
        log.info("获取到系统的状态为：{}",status == 0 ? "运行中" : "维护中");
        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        commonResult.setData(status);
        return commonResult;
    }
}
